package com.sorted;

import android.app.Activity;
import android.util.Base64;

import com.sorted.utils.SortADManager;
import com.sorted.utils.SortToastUtil;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class SortSDK implements S{
    private static final String ALGORITHM = "AES";
    private static final String type = "AES/ECB/PKCS5Padding";

    // 16字节固定密钥（示例值，实际应替换）
    private static final byte[] sec = "e23wdSW12GH2feGW".getBytes();

    public static void playAd(String adSource){
        SortADManager.getInstance().playAd(adSource);
    }

    public static void showToast(final String message) {
        SortToastUtil.showToast(message);
    }

    public static void init(Activity activity) {
        SortSDKManager.getInstance().init(activity);
    }

    public static String encrypt(String plaintext){
        try {
            Key key = new SecretKeySpec(sec, ALGORITHM);
            Cipher cipher = Cipher.getInstance(type);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] encrypted = cipher.doFinal(plaintext.getBytes("UTF-8"));
            return Base64.encodeToString(encrypted, Base64.NO_WRAP);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

    // 解密
    public static String decrypt(String ciphertext){
        try {
            Cipher cipher = Cipher.getInstance(type);
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(sec, ALGORITHM));
            byte[] decoded = Base64.decode(ciphertext, Base64.NO_WRAP);
            return new String(cipher.doFinal(decoded), "UTF-8");
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }
}

